﻿using System.Collections;
using UnityEngine;

public class SceneGame : MonoBehaviour
{
    private Node[,] snapshot;
    private int rowNumber = 20;
    private int colNumber = 20;
    private Node nodePrefab;
    private Snake snake;
    private int foodRow;
    private int foodCol;
    public bool IsGameOver;
    private SnakeDirection nodeDirection;
    private bool canLeft = true;
    private bool canRight = true;
    private bool canUp = true;
    private bool canDown = true;
    private void Awake()
    {
        Camera.main.transform.position = new Vector3((rowNumber + 1) / 2 - 0.5f, (colNumber + 1) / 2 - 0.5f, -10f);
        Camera.main.orthographicSize = rowNumber > colNumber ? rowNumber / 2 : colNumber / 2;
        InitSnapshot();
    }
    private void Start()
    {
        StartCoroutine("SnakeMove");
    }
    private void Update()
    {
        JudgeDirection();
    }
    private void JudgeDirection()
    {
        if (snake.GetBodyLength() >= 2)
        {
            var head = snake.GetBodyPosition(0);
            var second = snake.GetBodyPosition(1);
            var x = Mathf.Abs(head.x - second.x);
            var y = Mathf.Abs(head.y - second.y);
            if (x > 0.3f && y < 0.3f)
            {
                canLeft = false;
                canRight = false;
                canUp = true;
                canDown = true;
            }
            else if (x < 0.3f && y < 0.3f)
            {
                canLeft = false;
                canRight = false;
                canUp = true;
                canDown = true;
            }
            else if (x < 0.3f && y > 0.3f)
            {
                canLeft = true;
                canRight = true;
                canUp = false;
                canDown = false;
            }
        }
        if (Input.GetKeyDown(KeyCode.LeftArrow) && canLeft)
        {
            snake.SetDirection(SnakeDirection.Left);
        }
        if (Input.GetKeyDown(KeyCode.RightArrow) && canRight)
        {
            snake.SetDirection(SnakeDirection.Right);
        }
        if (Input.GetKeyDown(KeyCode.UpArrow) && canUp)
        {
            snake.SetDirection(SnakeDirection.Up);
        }
        if (Input.GetKeyDown(KeyCode.DownArrow) && canDown)
        {
            snake.SetDirection(SnakeDirection.Down);
        }
    }
    private void InitSnapshot()
    {
        //1、创建场景
        CreateBackground();
        //2、创建蛇
        CreateSnake();
        //3、创建食物
        CreateFood();
    }
    private void CreateBackground()
    {
        if (snapshot == null)
        {
            snapshot = new Node[rowNumber, colNumber];
        }
        if (nodePrefab == null)
        {
            nodePrefab = Resources.Load<Node>("Prefabs/NodePrefab");
        }
        for (int row = 0; row < rowNumber; row++)
        {
            for (int col = 0; col < colNumber; col++)
            {
                Node node = Instantiate(Resources.Load<Node>("Prefabs/NodePrefab"), transform);
                node.SetPosition(col, row);
                node.name = nodePrefab.name + row + col;
                snapshot[row, col] = node;

                if (row == 0 || row == rowNumber - 1 || col == 0 || col == colNumber - 1)
                {
                    snapshot[row, col].SetState(NodeState.Wall);
                }
                else
                {
                    snapshot[row, col].SetState(NodeState.Blank);
                }
            }
        }
    }
    private void CreateSnake()
    {
        if (snake == null)
        {
            snake = Instantiate(Resources.Load<Snake>("Prefabs/SnakePrefab"));
            snake.name = "Snake";
        }
        snake.gameObject.SetActive(true);
        snake.CreateSnake(rowNumber / 2, colNumber / 2);
        // snapshot[rowNumber / 2, colNumber / 2].SetDirection(snake.GetDirection());
    }
    public void CreateFood()
    {
        do
        {
            foodRow = UnityEngine.Random.Range(1, rowNumber - 1);
            foodCol = UnityEngine.Random.Range(1, colNumber - 1);
        } while (snapshot[foodRow, foodCol].GetState() != NodeState.Blank);
        UpdateSnapshot();
    }
    public void UpdateSnapshot()
    {
        for (int row = 1; row < rowNumber - 1; row++)
        {
            for (int col = 1; col < colNumber - 1; col++)
            {
                //1、清空快照
                snapshot[row, col].SetState(NodeState.Blank);
                snapshot[row, col].ClearRotation();
                //2、设置食物在快照中的位置
                if (row == foodRow && col == foodCol)
                {
                    snapshot[row, col].SetState(NodeState.Food);
                }
            }
        }
        //3、设置蛇在快照中的位置
        for (int index = 0; index < snake.GetBodyLength(); index++)
        {
            Vector2 snakePosition = snake.GetBodyPosition(index);
            snapshot[(int)snakePosition.y, (int)snakePosition.x].SetState(NodeState.Snake);
            snapshot[(int)snakePosition.y, (int)snakePosition.x].SetRotation();
        }
    }
    public Vector2 GetFoodPosition() => new Vector2(foodCol, foodRow);
    public Node GetNode(int x, int y)
    {
        return snapshot[x, y];
    }
    IEnumerator SnakeMove()
    {
        do
        {
            yield return new WaitForSeconds(0.3f);
            snake.Move();
            Vector2 snakePosition = snake.GetBodyPosition(0);
            snapshot[(int)snakePosition.y, (int)snakePosition.x].SetDirection(snake.GetDirection());
            UpdateSnapshot();
        } while (!IsGameOver);
    }
}